<template>
  <div class="comment-list">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <comment-item
        v-for="(item, index) in list"
        :key="index"
        :comment-info="item"
        @reply-click="$emit('reply-click', $event)"
      />
    </van-list>
  </div>
</template>

<script>
import { getComments } from '@/api/comment'
import CommentItem from './comment-item'
export default {
  name: 'CommentList',
  components: {
    CommentItem
  },
  props: {
    source: {
      type: [Number, String, Object],
      required: true
    },
    list: {
      type: Array,
      required: true
    },
    type: {
      type: String,
      default: 'a'
    }
  },
  data() {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },
  methods: {
    async onLoad() {
      // 异步更新数据
      const { data } = await getComments({
        // 评论类型 a 文章的回复，c 评论的回复
        type: this.type,
        // 源 id，文章 id 或者 评论 id
        source: this.source.toString(),
        // 评论数据偏移量
        offset: null,
        // 每页大小
        limit: 10
      })

      const { results, total_count: totalCount } = data.data
      this.list.push(...results)

      console.log(data.data)
      this.$emit('update-reply-count', totalCount)

      // 加载状态结束
      this.loading = false

      // 判断是否有数据
      // if (results.length) {
      // 如果有，更新获取下一页数据的页码
      // this.offset = data.data.last_id
      // } else {
      // 如果没有，则将 finished 设置为 true，不再触发加载更多
      this.finished = true
      // }
    }
  }
}
</script>

<style></style>
